/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snmpapplication.utils;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import snmpapplication.Main;
import snmpapplication.beans.ManagedDevice;
import snmpapplication.gui.AppMainWindow;
import snmpapplication.log.Severity;

/**
 * Class used to determine the reachability of the current list of devices
 * @author APetcu
 */
public class InitialReachability extends Thread {

    // variable for stoping the thread
    private boolean running = true;
    private JProgressBar jpb;
    // the frame that starts the service
    private JFrame invoker;

    public InitialReachability(JProgressBar jpb, JFrame invoker) {
        this.invoker = invoker;
        this.jpb = jpb;
        jpb.setMaximum(DevicesManager.deviceList.size());
        Main.log.log(new Severity(Severity.SEV_INFO), "Checking reachability of devices");
    }

    @Override
    public void run() {

        while (running) {
            for (ManagedDevice md : DevicesManager.deviceList) {
                if (Ping.pingMe(md.getIpAddress().toString())) {
                    // reachable by ping
                    md.setIsReachable(Constante.PING_REACH);
                } else {
                    // not reachable
                    md.setIsReachable(Constante.NO_REACH);
                }

                // increase the progress bar with 1
                jpb.setValue(jpb.getValue() + 1);

                // check if this was the last device
                if (jpb.getValue() == jpb.getMaximum()) {
                    running = false;
                    invoker.dispose();

                    Main.log.log(new Severity(Severity.SEV_INFO), "Checking reachability of devices DONE");
                    new Thread(new AppMainWindow()).start();

                }
            }
        }
    }

    public boolean isRunning() {
        return running;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }
}
